package com.yohu.blue.car;

import android.bluetooth.BluetoothGattCharacteristic;

/**
 * Created by huyong on 2017/10/16.
 */
public class BluetoothHelper {

    //    private Map<String, BluetoothDevice> map;
//
//    private static BluetoothGatt bluetoothGatt;
//    private Context context;
    private static BluetoothHelper bluetoothHelper;
    private static BluetoothLeService mBluetoothLeService;
    private static BluetoothGattCharacteristic characteristic;

    public static BluetoothHelper getInstance(BluetoothLeService mBluetoothLeService, BluetoothGattCharacteristic characteristic) {
//        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
//        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

//        if (!bluetoothAdapter.isEnabled()) {
//            if (!bluetoothAdapter.isEnabled()) {
//                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//                context.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
//            }
//        }
//        bluetoothAdapter.startLeScan(mLeScanCallback);
//        map = new HashMap<String, BluetoothDevice>();

        if (bluetoothHelper == null)
            bluetoothHelper = new BluetoothHelper();
        BluetoothHelper.mBluetoothLeService = mBluetoothLeService;
        BluetoothHelper.characteristic = characteristic;

        return bluetoothHelper;
    }

    public static void write(String content) {
        byte[] value = new byte[20];
        value[0] = (byte) 0x00;
        characteristic.setValue(value[0],
                BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        //write string
        characteristic.setValue(content.getBytes());

        mBluetoothLeService.writeCharacteristic(characteristic);
    }

//    // Device scan callback.
//    private BluetoothAdapter.LeScanCallback mLeScanCallback =
//            new BluetoothAdapter.LeScanCallback() {
//
//                @Override
//                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
//                    System.out.println("device:" + device.getAddress());
//                    if (map.get(device.getAddress()) == null) {
//                        map.put(device.getAddress(), device);
//                    }
//                    bluetoothGatt = device.connectGatt(context, false, new BluetoothGattCallback() {
//                        @Override
//                        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//                            super.onConnectionStateChange(gatt, status, newState);
//                            List<BluetoothGattService> bluetoothGattServices = gatt.getServices();
//
//                            System.out.println(bluetoothGattServices.size());
//                        }
//                    });
//                    bluetoothGatt.connect();
//                    BluetoothGattService bluetoothGattService = bluetoothGatt.getServices().get(0);
//
////                    BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattService.getCharacteristic("");
////                    bluetoothGattCharacteristic.setValue("a");
////                    bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic);
//                    System.out.println(bluetoothGattService.getUuid());
//                }
//            };
}
